package com.lzb.randomturntable.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.basic.utils.SPUtil;
import com.lzb.randomturntable.BaseAc;
import com.lzb.randomturntable.Content;
import com.lzb.randomturntable.R;
import com.lzb.randomturntable.adapter.TurntableAdapter;
import com.lzb.randomturntable.bean.TurntableBean;
import com.lzb.randomturntable.databinding.ActivityTurntableListBinding;
import com.lzb.randomturntable.utils.AppSpUtils;

import java.util.ArrayList;
import java.util.List;

public class TurntableListActivity extends BaseAc<ActivityTurntableListBinding> implements View.OnClickListener, OnItemClickListener, OnItemChildClickListener {

    private TurntableAdapter turntableAdapter;
    private List<TurntableBean> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_turntable_list;
    }

    @Override
    protected void initView() {
        mDataBinding.ivTurntableListBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.ivAdd.setOnClickListener(this);

        mDataBinding.rvTurntableList.setLayoutManager(new LinearLayoutManager(mContext));
        turntableAdapter = new TurntableAdapter();
        mDataBinding.rvTurntableList.setAdapter(turntableAdapter);
        turntableAdapter.addChildClickViewIds(R.id.ivTurntableItemCopy);
        turntableAdapter.addChildClickViewIds(R.id.ivTurntableItemEdit);
        turntableAdapter.addChildClickViewIds(R.id.ivTurntableItemDelete);
        turntableAdapter.setOnItemClickListener(this);
        turntableAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivAdd) {//添加
            AddTurntableActivity.selPosition = -1;
            startActivity(AddTurntableActivity.class);
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        SPUtil.putInt(mContext, Content.turntablePos, position);
        turntableAdapter.notifyDataSetChanged();
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        int id = view.getId();
        if (id == R.id.ivTurntableItemCopy) {//复制
            AddTurntableActivity.selPosition = position;
            Intent bundle = new Intent(this, AddTurntableActivity.class);
            bundle.putExtra(Content.isCopy, true);
            startActivity(bundle);
        } else if (id == R.id.ivTurntableItemEdit) {//编辑
            AddTurntableActivity.selPosition = position;
            startActivity(AddTurntableActivity.class);
        } else if (id == R.id.ivTurntableItemDelete) {//删除
            listShow.remove(position);
            int turntablePos = SPUtil.getInt(mContext, Content.turntablePos, 0);
            if (position == turntablePos) {
                SPUtil.putInt(mContext, Content.turntablePos, 0);
            }
            AppSpUtils.saveTurntableList(listShow);
            ToastUtils.showShort(getString(R.string.delete_suc));
            getData();
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow = AppSpUtils.getTurntableList();
        if (listShow != null && listShow.size() > 0) {
            turntableAdapter.setList(listShow);
            mDataBinding.rvTurntableList.setVisibility(View.VISIBLE);
            mDataBinding.tvTurntableNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvTurntableList.setVisibility(View.GONE);
            mDataBinding.tvTurntableNoData.setVisibility(View.VISIBLE);
        }
    }
}